import 'dart:io';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../services/version_manger.dart';
import '../../models/version_model.dart';

class NewVersionView extends StatefulWidget {
  final NewVersionModel model;

  const NewVersionView({super.key, required this.model});

  @override
  State<NewVersionView> createState() => _NewVersionViewState();
}

class _NewVersionViewState extends State<NewVersionView> {
  bool isVisible = true;

  @override
  Widget build(BuildContext context) {
    if (!isVisible) return const SizedBox.shrink();

    final model = widget.model;

    return Scaffold(
      backgroundColor: Colors.black.withValues(alpha: 0.4),
      body: Center(
        child: Container(
          width: 300,
          height: 265,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(18),
          ),
          child: Stack(
            children: [
              Column(
                children: [
                  // 顶部图
                  Stack(
                    children: [
                      Image.asset(
                        'assets/images/newversion2.png',
                        height: 77,
                        width: 300,
                        fit: BoxFit.cover,
                      ),
                      Positioned(
                        right: 30,
                        top: -30,
                        child: Image.asset(
                          'assets/images/newversion1.png',
                          width: 100,
                          height: 100,
                        ),
                      ),
                      Positioned(
                        left: 25,
                        top: 15,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(model.pushTitle, style: const TextStyle(fontSize: 19, fontWeight: FontWeight.bold)),
                            const SizedBox(height: 3),
                            Text(model.version, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                          ],
                        ),
                      ),
                    ],
                  ),

                  const SizedBox(height: 20),

                  // 描述
                  SizedBox(
                    width: 250,
                    height: 100,
                    child: Text(
                      model.pushText,
                      style: const TextStyle(fontSize: 16, color: Colors.grey),
                    ),
                  ),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      if (model.forcedUpdate == 0)
                        _buildButton(
                          text: model.cancelButton,
                          color: const Color(0xFFEFEFEF),
                          textColor: Colors.black87,
                          onTap: () {
                            Navigator.pop(context);
                            VersionManager.saveShownVersion(model.version);
                          },
                        ),
                      _buildButton(
                        text: model.upgradeButton,
                        color: const Color(0xFF5D5FEE),
                        textColor: Colors.white,
                        onTap: _launchStore,
                        isFullWidth: model.forcedUpdate == 1,
                      ),
                    ],
                  ),
                  const SizedBox(height: 10),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildButton({
    required String text,
    required Color color,
    required Color textColor,
    required VoidCallback onTap,
    bool isFullWidth = false,
  }) {
    return SizedBox(
      width: isFullWidth ? 220 : 125,
      height: 42,
      child: ElevatedButton(
        onPressed: onTap,
        style: ElevatedButton.styleFrom(
          backgroundColor: color,
          foregroundColor: textColor,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(21),
          ),
        ),
        child: Text(text, style: const TextStyle(fontSize: 16)),
      ),
    );
  }

  Future<void> _launchStore() async {
    final url = Platform.isIOS
        ? 'https://apps.apple.com/app/id6739838354'
        : 'https://play.google.com/store/apps/details?id=com.ninetyfour.app'; // 替换为你的 Android 包名

    if (await canLaunchUrl(Uri.parse(url))) {
      await launchUrl(Uri.parse(url), mode: LaunchMode.externalApplication);
    } else {
      debugPrint("无法打开应用商店链接");
    }
  }
}
